require(["../../conf/config.js"],function(){
	require(["jquery", "jqcookie"], function($){
		$(function(){ 
			var timer; 
			$("#kefu a").mouseenter(function(){
				$("#kefu ul").animate({"opacity": 1}, 100);
			})
			$("#kefu").mouseleave(function(){
				timer = setTimeout(function(){
					$("#kefu ul").animate({"opacity": 0}, 100);
				}, 200);
			})  
			$("#kefu ul").mouseenter(function(){
				clearTimeout(timer);
			})
			$("#kefu ul").mouseleave(function(){
				$("#kefu ul").animate({"opacity": 0}, 100);
			}) 
		}) 
		function inputfocusedStyle(selector){
			let content = "";
			$(selector).on("focus", function(){
				if(selector == "#phonenum" || selector == "oserpassword"){
					pfocusStyle(selector);
				}
				
				content = $(this).val();
				console.log(content);
				$(this).val("");
				return content;
			})
			
		}
		function pBlurStyle(selector){
			if(selector == "#phonenum"){
				console.log(selector);
				$("#phonenum ~ p").animate({opacity: 1});
			} else if(selector == "#userpassword"){
				$("#userpassword ~ p").animate({opacity: 1});
			}
		}
		function pfocusStyle(selector){
			if(selector == "#phonenum"){
				$("#phonenum ~ p").animate({opacity: 0});
			} else if(selector == "#userpassword"){
				$("#userpassword ~ p").animate({opacity: 0});
			}
		}
		function inputIsEmpty(selector){
			if($(selector).val() == ""){
				return true;
			}else{
				return false;
			}
		}
		let arr = [];
		function emailJudge(){
			let flag = false;
			let content = inputfocusedStyle("#email");
			$("#email").on("blur",function(){
				if($(this).val() == ""){
					$(this).val(content);
					$.cookie("email","");
				}else{
					let nowContent = $(this).val();
					if(!/^\S+@\S+\.\S{2,}$/.test(nowContent)){
						alert("邮箱格式错误");
						arr.push(flag);
					}else{
						$.cookie("email", nowContent);
						flag = true;
						arr.push(flag);
					}
				}
			})
		}
		emailJudge();
		function phoneNumJudge(){
			let flag = false;
			let content = inputfocusedStyle("#phonenum");
			$("#phonenum").on("blur", function(){
				console.log("ok");
				if($(this).val() == ""){
					pBlurStyle("#phonenum");
					$(this).val(content);
					arr.push(flag);
				}else{
					let nowContent = $(this).val();
					if(!/^1[34578]\d{9}$/.test(nowContent)){
						alert("电话号码错误");
						arr.push(flag);
					}else{
						$.cookie("username", nowContent);
						flag = true;
						arr.push(flag);
					}
				}
			})
		}
		phoneNumJudge();
		function verifyJudge(){
			let code = verifyCode();
			let flag = false;
			let content = inputfocusedStyle("#verification");
			$("#vericode b").text(code);
			$("#verification").on("blur", function(){
				if($(this).val() == ""){
					alert("请填写验证码");
					let code = verifyCode();
					$("#vericode b").text(code);
					$(this).val(content);
					arr.push(flag);
				}else{
					let nowContent = $(this).val();
					if(nowContent != $("#vericode b").text()){
						alert("验证码错误");
						arr.push(flag);
					}else{
						flag = true;
						arr.push(flag);
					}
				}
			})
		}
		function changeCode(){
			$("#vericode button").on("click", function(e){
				stopDefault( e );
				let code = verifyCode();
				$("#vericode b").text(code);
			})
		}
		verifyJudge();
		function passwordJudge(){
			let flag = false;
			let content = inputfocusedStyle("#verification");
			$("#userpassword").on("blur", function(){
				if($("#userpassword").val() == ""){
					alert("请输入密码");
					$(this).val(content);
					arr.push(flag);
				}else{
					$.cookie("password", $(this).val());
					flag = true;
					arr.push(flag);
				}
			})
		}
		passwordJudge(); 
		function passwordAffirm(){
			let flag = false;
			let content = inputfocusedStyle("#verification");
			$("#passwordaffirm").on("blur", function(){
				if($("#userpassword").val() == ""){
					alert("请再次输入密码");
					$(this).val(content);
					arr.push(flag);
				}else{
					let nowContent = $(this).val();
					if( nowContent != $("#userpassword").val()){
						alert("密码错误");
						$("#userpassword").val("");
						arr.push(flag);
					}else{
						flag = true;
						arr.push(flag);
					}
				}
			})
		}
		passwordAffirm();
		
		function userRegister(){
			$("#registerButton").on("click",function(e){
				stopDefault( e )
				console.log("aaa");
				console.log($("#checkBox").get(0).checked);
				if($("#checkBox").get(0).checked == true){
					console.log("ok");
					for(let i in arr){
						if(arr[i] = false){
							alert("请完善信息");
							
						}
					}
					alert("注册成功");
				}else{
					alert("请先阅读注册协议");
				}
			})
		}
		userRegister();
		function verifyCode(){
			let t = "";
			for (let i = 0; i < 4; i++){
				let num = parseInt(Math.random()*10);
				t += num;
			}
			return t;
		}
			
		//阻止浏览器的默认行为 
		function stopDefault( e ) { 
		    //阻止默认浏览器动作(W3C) 
		    if ( e && e.preventDefault ){
				 e.preventDefault(); 
			} 
		    //IE中阻止函数器默认动作的方式 
		    else {
				window.event.returnValue = false;
			}
		    return false; 
		}
		
	})
})  